Conditions | 12 |
Paths | 112 |
Total Lines | 66 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like generate_decay.js ➔ calculateReaction often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /* eslint-env node */ |
||
75 | function calculateReaction(isotope, number, particle, protonDifference, isotopeDifference) { |
||
76 | let element = isotope.replace(/^[0-9]*/, ''); |
||
77 | let elementNumber = elements[element].number - 1; |
||
78 | let listElements = Object.keys(elements); |
||
79 | let otherElement = listElements[elementNumber + protonDifference]; |
||
80 | |||
81 | let isotopeNumber = Number(isotope.replace(element, '')) + isotopeDifference; |
||
82 | let product = isotopeNumber + otherElement; |
||
83 | |||
84 | // We need all this convoluted logic to work around missing isotopes in the data set |
||
85 | // essentially we look for the closest isotope to the target one, and consume/produce |
||
86 | // free neutrons in the process |
||
87 | let distance = 0; |
||
88 | if (!resources[product]) { |
||
89 | let candidate = null; |
||
90 | // first we start looking for lighter isotopes |
||
91 | for (let otherNumber = isotopeNumber; otherNumber > 0; otherNumber--) { |
||
92 | let otherProduct = otherNumber + otherElement; |
||
93 | if (resources[otherProduct]) { |
||
94 | candidate = otherProduct; |
||
95 | distance = isotopeNumber - otherNumber; |
||
96 | break; |
||
97 | } |
||
98 | } |
||
99 | // pay attention to the upper bound. 300 is bigger than any known isotope, so it is safe |
||
100 | for (let otherNumber = isotopeNumber; otherNumber < 300; otherNumber++) { |
||
101 | let otherProduct = otherNumber + otherElement; |
||
102 | if (resources[otherProduct]) { |
||
103 | // we only replace the candidate if the distance is smaller |
||
104 | if (isotopeNumber - otherNumber < Math.abs(distance)) { |
||
105 | candidate = otherProduct; |
||
106 | distance = isotopeNumber - otherNumber; |
||
107 | } |
||
108 | break; |
||
109 | } |
||
110 | } |
||
111 | if (!candidate) { |
||
112 | throw new Error('No candidate found for ' + isotope + ' replacing the missing isotope ' + product); |
||
113 | } |
||
114 | product = candidate; |
||
115 | } |
||
116 | let energy = resources[isotope].energy - resources[product].energy; |
||
117 | if (distance < 0) { |
||
118 | energy -= resources.n.energy * distance; |
||
119 | } |
||
120 | |||
121 | let reaction = {}; |
||
122 | reaction.reactant = {}; |
||
123 | reaction.reactant[isotope] = number; |
||
124 | // if the isotope is heavier, the distance is negative and we produce neutrons |
||
125 | if (distance < 0) { |
||
126 | reaction.reactant.n = Math.abs(distance); |
||
127 | } |
||
128 | reaction.product = {}; |
||
129 | reaction.product[product] = number; |
||
130 | if (particle) { |
||
131 | reaction.product[particle] = number; |
||
132 | } |
||
133 | // if the isotope is lighter, the distance is positive and we produce neutrons |
||
134 | if (distance > 0) { |
||
135 | reaction.product.n = distance; |
||
136 | } |
||
137 | reaction.product.eV = energy; |
||
138 | |||
139 | return reaction; |
||
140 | } |
||
141 | |||
192 |